package com.naihe.anderson.sms.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.entity.NhsmsSession;
import com.naihe.anderson.sms.listener.OnMenuDialogClickListener;
import com.naihe.anderson.sms.util.MyAnimations;

public class MenuDialog extends Dialog {

	OnMenuDialogClickListener menuDialogClickListener;
	NhsmsSession mNhsms;
	
	public MenuDialog(Context context) {
		super(context);
	}

	public MenuDialog(Context context, int theme, OnMenuDialogClickListener listener, NhsmsSession nhsms) {
		super(context, theme);
		menuDialogClickListener = listener;
		mNhsms = nhsms;
	}

	protected MenuDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_layout);
		
		LinearLayout menuCenter = (LinearLayout) findViewById(R.id.menu_center);
		menuCenter.startAnimation(MyAnimations.getRotateAnimation(0,-360, 300));
		Button menuMidding = (Button) findViewById(R.id.menu_midding);
		menuMidding.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_center));
		Button menuLeft = (Button) findViewById(R.id.menu_left);
		menuLeft.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_left));
		Button menuRight = (Button) findViewById(R.id.menu_right);
		if(mNhsms.getPerson() <= 0){
			menuRight.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_right_2));
		} else {
			menuRight.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_right_1));
		}
		
		Button menuTop = (Button) findViewById(R.id.menu_top);
		if(TextUtils.isEmpty(mNhsms.getCipher())){
			menuTop.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_top_1));
		} else {
			menuTop.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_top_2));
		}
		
		Button menuCopy = (Button) findViewById(R.id.menu_copy);
		menuCopy.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_copy));
		
		Button menuBottom = (Button) findViewById(R.id.menu_bottom);
		menuBottom.setText(getContext().getResources().getString(R.string.menu_btn_text_menu_bottom));
		
		menuCopy.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ClipboardManager clipBoard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
				clipBoard.setText(mNhsms.getAddress());
				onClose();
			}
		});
		menuMidding.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menuDialogClickListener.onOpenSessionItem(mNhsms);
				onClose();
			}
		});
		menuLeft.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menuDialogClickListener.onMakeCall(mNhsms);
				onClose();
			}
		});
		menuRight.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menuDialogClickListener.onOpenItemToCantact(mNhsms);
				onClose();
			}
		});
		menuTop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menuDialogClickListener.onCollectionSession(mNhsms);
				onClose();
			}
		});
		menuBottom.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menuDialogClickListener.onDelNhSmsSession(mNhsms);
				onClose();
			}
		});
	}
	
	private void onClose(){
		this.cancel();
		this.dismiss();
	}
	

}
